package com.nettest.action.system;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.nettest.action.BaseJsonAction;
import com.nettest.entity.system.Menu;
import com.nettest.entity.system.Role;
import com.nettest.service.event.RoleChangedEvent;
import com.nettest.service.system.MenuService;
import com.nettest.service.system.RoleService;
import com.nettest.util.StringUtils;

@Action("roleMenuAuth")
@Namespace("/system/role")
@Results({
	@Result(name = "success", location = "/system/roleMenuAuth.jsp") })
public class RoleMenuAuthAction extends BaseJsonAction implements ApplicationContextAware{

    private static final long serialVersionUID = 2624526814419100800L;

    private String menuTree;
    private String roleId;
    private String menuIds;
    private String operate = " ";
    @Autowired
    private RoleService roleService ;
    @Autowired
    private MenuService menuService ;
    private ApplicationContext applicationContext;
    
    @Action(value = "menuAuth",results={@Result(name = "success", location = "/system/menuAuth.jsp")})
    public String menuAuth() throws Exception  {
    	List<String> menuIds = roleService.queryMenuIdByRoleId(roleId);
        StringBuilder jsstr = new StringBuilder("<ul class=\"easyui-tree\" id=\"menuTree\" data-options=\"fit:true,checkbox:true\">");
        jsstr.append("<li id=\"0\"><span>菜单</span>");
        List<Menu> allTopMenus = menuService.getTopMenu(null);
        jsstr.append(buildMenuTree(allTopMenus,menuIds));
        jsstr.append("</li></ul>");
        menuTree = jsstr.toString();
        System.out.println(menuTree);
        return SUCCESS;
    }
    
    protected String buildMenuTree(List<Menu> menus,List<String> menuIds) {
    	StringBuilder jsstr = new StringBuilder("<ul>");
    	for (Menu menu : menus) {
    		jsstr.append("<li data-options=\" id:'" + menu.getMenuId() + "',checked: " + menuIds.contains(menu.getMenuId()) +" \"><span>" + menu.getMenuName() +"</span>");
    		List<Menu> subMenus = menuService.getSubMenu(menu.getMenuId());
    		if(!subMenus.isEmpty()) {
    			jsstr.append(buildMenuTree(subMenus,menuIds));
    		} 
   			jsstr.append("</li>");
		}
    	jsstr.append("</ul>");
    	return jsstr.toString();
    }
    
    @Action(value = "saveRoleMenuAuth")
    public String saveRoleMenuAuth() throws Exception {
    	if (!StringUtils.isBlank(roleId)) {
    		Role role = roleService.get(roleId);
    		Set<Menu> menuSet = new HashSet<Menu>();
    		if (!StringUtils.isBlank(menuIds)) {
    			String[] menuIdArr = menuIds.split(";");
    			for (String menuId : menuIdArr) {
    			    if (!StringUtils.isBlank(menuId)) {
    			    	Menu menu = menuService.getMenuById(menuId);
    			    	if(menu != null) {
    			    		menuSet.add(menu);
    			    	}
    			    }
				}
    		} 
    		role.setMenus(menuSet);
    		roleService.save(role);
    		applicationContext.publishEvent(new RoleChangedEvent(this));
    	}
    	return null;
    }

    public String getMenuTree() {
        return menuTree;
    }

    public void setMenuTree(String menuTree) {
        this.menuTree = menuTree;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	public String getOperate() {
		return operate;
	}
	public void setOperate(String operate) {
		this.operate = operate;
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}
